var alist = "#alist", aname = "#aname", arepName = "#arepName", ascriptName = "#ascriptName", ascriptType = "#ascriptType", kform = "#kform";

function alistLoaderComplete(resultData) {
	datagridReqErr(this, resultData, 'name', 9);
	$(".k1Oper").linkbutton({ height: 23, plain: true });
}

function alistPage(param) {
	param.pgCt = param.page;
	param.pgSz = param.rows;
	delete param.page;
	delete param.rows;
	return param;
}

function alistLoader(param, success, error) {
	let tname = $(aname).textbox("getValue");
	let trepName = $(arepName).textbox("getValue");
	let tscriptName = $(ascriptName).textbox("getValue");
	let tscriptType = $(ascriptType).combobox("getValue");
	if ("" != $.trim(tname)) param.name = tname;
	if ("" != $.trim(trepName)) param.repName = trepName;
	if ("" != $.trim(tscriptName)) param.scriptName = tscriptName;
	if ("" != $.trim(tscriptType)) param.scriptType = tscriptType;
	
	ajaxPost(reqRoute.taskPage, alistPage(param), function(resultData) {
		success(resultData);
	});
}

// 查询
function asearch() {
	$(alist).datagrid('load', {});
}

// 重置
function areset() {
	$(aname).textbox("clear");
	$(arepName).textbox("clear");
	$(ascriptName).textbox("clear");
	$(ascriptType).combobox("clear");
	$(alist).datagrid('load', {});
}

// 启动
function astart(id) {
	$.get(reqRoute.taskStart, { id: id }, function(resultData) {
		if (resultData.status == successCode) {
			asearch();
		} else {
			$.messager.alert("提示", resultData.message, "error");
		}
	});
}

// 编辑
function aeditPage(id) {
	var taeditId = $("#aeditId").val();
	if (taeditId) {
		$("#aeditId").remove();
	}
	$("body").append('<input id="aeditId" value="' + id + '">');
	$(kcenter).panel({ href: pageRoute.taskEdit });
}

// 删除
function adelete(id, taskName) {
	$.messager.confirm('提示', '确定要删除[' + taskName + ']吗？', function(r) {
	    if (r) {
	    	$.get(reqRoute.taskDel, { id: id }, function(resultData) {
	    		if (resultData.status == successCode) {
	    			asearch();
	    		} else {
	    			$.messager.alert("提示", resultData.message, "error");
	    		}
	    	});
	    }
	});
}

// 停止
function astop(id) {
	$.get(reqRoute.taskStop, { id: id }, function(resultData) {
		if (resultData.status == successCode) {
			asearch();
		} else {
			$.messager.alert("提示", resultData.message, "error");
		}
	});
}

// 执行记录
function ahistory(id) {
	let fcolumns = [[
		{field:'startTime', title:'起始时间', halign:'center', width:140},
		{field:'stopTime', title:'结束时间', halign:'center', width:140},
		{field:'recordStatus', title:'执行结果', halign:'center', width:70, formatter:function(value, row, index){
			if ("N" == value) return '<span style="color:red">失败</span>';
			if ("Y" == value) return '<span style="color:#00AA00">成功</span>';
			if ("W" == value) return '等待';
			return "";
		}},
		{field:'logFilePath', title:'日志记录文件', halign:'center', width:335},
		{field:'id', title:'操作', halign:'center', width:70, formatter:function(value, row, index){
			return '<a class="arec" href="javascript:t1RecDown(\''+row.logFilePath+'\')"><i class="fa fa-download fa-lg"></i> 下载</a>';
		}}
	]];
	datagridInitER("#arecDiaList", fcolumns, function(param, success, error) {
		param.taskId = id;
		ajaxPost(reqRoute.taskRecordPage, alistPage(param), function(resultData) {
			success(resultData);
		});
	}, function(resultData) {
		datagridReqErr(this, resultData, 'startTime', 5);
		$(".arec").linkbutton({ height: 23, plain: true });
	});
	$("#arecDia").dialog('open');
}

// 下载执行日志文件
function t1RecDown(filePath) {
	window.location.href = reqRoute.taskRecordDown + "?filePath=" + filePath;
}

// (新增，编辑)取消
function bcclear() {
	$(kcenter).panel({ href: pageRoute.taskList });
}

// (新增，编辑)参数渲染
function bcparams() {
	let bcparam = ".bcparam";
	let seqv = parseInt($(bcparam).attr("bcseq")) + 1;
	
	var html = '<div id="bcdiv'+seqv+'" style="margin-top:10px">';
	if (seqv == 1) html = '<div id="bcdiv'+seqv+'">';
	html += '<input id="bckey'+seqv+'">--';
	html += '<input id="bcvalue'+seqv+'">&nbsp;';
	html += '<a id="bca'+seqv+'" href="javascript:bcparams()"></a>';
	if (seqv != 1) html += '<a id="bcar'+seqv+'" href="javascript:bcparamRem(\'bcdiv'+seqv+'\')"></a>';
	html += '<div>';
	$(bcparam).append(html);
	
	if (seqv == 1) {
		$("#bckey"+seqv).textbox({label:'参数',labelAlign:'right',cls:'bcparam-key'});
	} else {
		$("#bckey"+seqv).textbox({label:'  ',labelAlign:'right',cls:'bcparam-key'});
		{ $("#bcar"+seqv).linkbutton({plain:true,iconCls:'icon-remove'}); }
	}
	$("#bcvalue"+seqv).textbox({cls:'bcparam-value'});
	$("#bca"+seqv).linkbutton({plain:true,iconCls:'icon-add'});
	
	$(".bcparam-key").css("width", "25%");
	$(".bcparam-value").css("width", "25%");
	$(".bcparam-key>input").css("width", "99%");
	$(".bcparam-value>input").css("width", "99%");
	
	$(bcparam).attr("bcseq", seqv);
}

// (新增，编辑)参数移除
function bcparamRem(divId) {
	$("#"+divId).remove();
}

// (编辑)参数渲染
function cviewParam(params) {
	if (params.length == 0) {
		bcparams();
		return;
	}
	
	$(params).each(function(index, item){
		bcparams();
		$("#bckey"+(index+1)).textbox("setValue", item.pkey);
		$("#bcvalue"+(index+1)).textbox("setValue", item.pvalue);
	});
}

// (新增，编辑)cron解析
function bcparserCron() {
	$("#bccron").textbox('options').onClickButton = function() {
		var bccronDia = "#bccronDia";
		$.get(reqRoute.parseCron, { cron: $(this).textbox('getValue') }, function(resultData) {
			var html = "";
			if (resultData.status == successCode) {
				$(resultData.data).each(function(index, item) {
					html += "<p style='text-align:center;'>" + item + "</p>"
				});
			} else {
				html += "<p style='color:red;text-align:center;'>" + resultData.message + "</p>"
			}
			$("#bccronDiaHtml").html(html);
			$(bccronDia).dialog('open');
		});
	};
}

// (新增，编辑)保存
function bcsubmit(flag) {
	var tmpUrl = (flag ? reqRoute.taskAdd : reqRoute.taskEdit);
	let bcseq = parseInt($(".bcparam").attr("bcseq"));
	let tmap = new Map(), tformData = formData(kform);
	
	if (bcseq != 0) {
		for(let i = 1; i <= bcseq; i++) {
			try {
				let tkeyv = $("#bckey"+i).textbox("getValue");
				let tvaluev = $("#bcvalue"+i).textbox("getValue");
				if ($.trim(tkeyv) != '') tmap.set(tkeyv, tvaluev);
			} catch(e) {}
		}
		tformData.params = [];
		tmap.forEach(function(value, key, map){
			tformData.params.push({pkey:key,pvalue:value});
		});
	}
	
	if ($(kform).form('enableValidation').form('validate')) {
		ajaxPost(tmpUrl, tformData, function(resultData) {
			if (resultData.status == successCode) {
				bcclear();
			} else {
				$.messager.alert("提示", resultData.message, "error");
			}
		});
	}
}

function ktaskEditPage(id) {
	var krepIdVal = $(keditRepId).val();
	if (krepIdVal) $(keditRepId).remove(); 
	$("body").append("<input id='"+keditRepId.substr(1)+"' value='"+id+"'>");
	
	$(kcenter).panel({ href: pageRoute.taskEdit });
}